Opackage com.atomos.strategy.action.position
{
	import com.atomos.utils.PointUtils;	
	
	import flash.geom.Point;
	import flash.utils.Dictionary;
	
	import com.atomos.particle.Particle;
	import com.atomos.strategy.action.AbstractAction;
	import com.atomos.utils.MathUtils;
	import com.atomos.utils.RandomUtils;	

	public class PerpendicularToVelocitySineMoveAction extends AbstractAction
	{
		protected var _dData : Dictionary;
		
		protected var _nAmplitude : Number;
		protected var _nFrequency : Number;
		
		protected var _nRandomAmplitude : Number;
		protected var _nRandomFrequency : Number;
		protected var _nRandomStart : Number;
		
		public function PerpendicularToVelocitySineMoveAction ( amplitude : Number = 0, frequency : Number = 1, randomStart : Number = 0, randomAmplitude : Number = 0, randomFrequency : Number = 0 )
		{
			_dData = new Dictionary ( true );
			
			_nAmplitude = amplitude;
			_nFrequency = frequency * MathUtils.PI2;
			
			_nRandomStart = randomStart;
			_nRandomAmplitude = randomAmplitude;
			_nRandomFrequency = randomFrequency;
		}
		
		public override function process( particle : Particle ) : void
		{
			if( !_dData [ particle ] )
			{
				_dData [ particle ] = new SineData( 
											particle.position.clone(),
											RandomUtils.random( _nRandomStart ),
											_nAmplitude + RandomUtils.randomBalance( _nRandomAmplitude ),
											_nFrequency + RandomUtils.randomBalance( _nRandomFrequency ) );
			}
			particle.lastPosition.x = particle.position.x;			particle.lastPosition.y = particle.position.y;
			
			var data : SineData = _dData [ particle ] as SineData;
			var pos : Point = data.position;
			var norm : Point = new Point( particle.velocity.y, particle.velocity.x );
			
			norm.normalize( data.amplitude * Math.sin( data.sine ) );
			
			pos.x += particle.velocity.x * _nTimeStep;
			pos.y += particle.velocity.y * _nTimeStep;
			
			particle.position = pos ;
			
			data.sine += data.frequency * _nTimeStep; 
		}
	}
}

import flash.geom.Point;

internal class SineData
{
	public var sine : Number;
	public var position : Point;
	public var amplitude : Number;
	public var frequency : Number;
	
	public function SineData( position : Point, sine : Number = 0, amplitude : Number = 1, frequency : Number = 1 )
	{
		this.sine = sine;
		this.position = position;
		this.frequency = frequency;
		this.amplitude = amplitude;
	}
}